class School:
    def __init__(self):
        self.departments = []  # 学院包含多个系
        self.students = []     # 学院拥有多个学生

    def add_department(self, department):
        self.departments.append(department)

    def add_student(self, student):
        self.students.append(student)


class Department:
    def __init__(self, name):
        self.name = name
        self.instructors = []  # 每个系至少有 1 位指导教师
        self.subjects = []     # 每个系提供至少 1 门课程

    def add_instructor(self, instructor):
        self.instructors.append(instructor)
        instructor.department = self  # 设置教师的系

    def add_subject(self, subject):
        self.subjects.append(subject)


class Instructor:
    def __init__(self, name):
        self.name = name
        self.department = None    # 每个教师至少属于某个系
        self.subjects = []        # 教师教授 1 到 3 门课程

    def add_subject(self, subject):
        if len(self.subjects) < 3:
            self.subjects.append(subject)
            subject.instructor = self  # 设置课程的教师
        else:
            raise ValueError("Each instructor can only teach up to 3 subjects.")


class Subject:
    def __init__(self, name):
        self.name = name
        self.instructor = None    # 每门课程由一个教师教授


class Student:
    def __init__(self, name):
        self.name = name
        self.subjects = []        # 每个学生学习 1 到 5 门课程

    def enroll(self, subject):
        if len(self.subjects) < 5:
            self.subjects.append(subject)
        else:
            raise ValueError("Each student can enroll in up to 5 subjects.")

